using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Madason.GenUtils
{
	public enum FileSelectorMode
	{
		SelectFile,
		SelectDir
	}

	public class FileSelector : TableLayoutPanel
	{
		TextBox textbox;
		FileSelectorMode mode;


		public string Text
		{
			get
			{
				return textbox.Text;
			}
			set
			{
				textbox.Text = value;
			}
		}

		public FileSelector(FileSelectorMode mode)
		{
			this.mode = mode;
			ColumnCount = 2;
			textbox = new TextBox();
			Controls.Add(textbox);
			Button button = new Button();
			button.Width = 20;
			button.Text = "...";
			Controls.Add(button);
			button.Click += new EventHandler(button_Click);
			Height = 30;
			
			this.SizeChanged += new EventHandler(FileSelector_SizeChanged);
		}

		void FileSelector_SizeChanged(object sender, EventArgs e)
		{
			textbox.Width = Width - 40;
		}

		void button_Click(object sender, EventArgs e)
		{
			if (mode == FileSelectorMode.SelectDir)
			{
				FolderBrowserDialog dlg = new FolderBrowserDialog();

				if (textbox.Text == "")
					dlg.SelectedPath = new System.IO.DirectoryInfo(".").FullName;
				else
					dlg.SelectedPath = new System.IO.DirectoryInfo(textbox.Text).FullName;


				if (dlg.ShowDialog() == DialogResult.OK)
				{
					textbox.Text = dlg.SelectedPath;
				}
			}
			else if (mode == FileSelectorMode.SelectFile)
			{
				OpenFileDialog dlg = new OpenFileDialog();
				dlg.FileName = textbox.Text;
				if (dlg.ShowDialog() == DialogResult.OK)
				{
					textbox.Text = dlg.FileName;
				}
			}
		}


	}
}
